function Data(){
	var _this = this;
	this.segment = { start:0, end:0, back:0, last:0};
	this.blank = false;
	this.metas = [];
	this.error = false;
	this.file = null;
	this.packs = [];
	this.graph = null;
	this.reload = function(){
		if (this.graph !=null) this.graph.curves=[];
		var reader = new FileReader();
		if (this.blank) {
			alert('blanck');
			this.segment.start = 0;
			this.segment.back = 0;
		}
		if (this.segment.back !=0) {
			//appa("SEGMENT BACK! ="+ this.segment.back);
			this.segment.start-=this.segment.back;
			this.segment.back = 0;
		}
		var blob = this.file.slice(this.segment.start, this.file.size);
		this.segment.start=this.file.size-1;
		reader.onload = this.import;
		reader.readAsText(blob);
		
	}
	this.import = function (e){
		//$("#monitor").empty();
		var blob = _this.splitJsons(e.target.result);
		
		for (var i=0; i<blob.length; i++){
			var pack = {};
			//_this.sleep(1000);
			try { 
				pack = $.parseJSON(blob[i]);
				_this.segment.last = blob[i].length;
				semafor(true);
				//print(blob[i]);
				
			}
			catch(err) {
				print("PARSE ERROR! - "+blob[i]);
//				printa(err);
				pack.type = 'error';
				this.error = true;
				semafor(false);
				_this.segment.back=blob[i].length+_this.segment.last;
				_this.graph.reset();
				return;
			}
			if(pack.type=='meta' && !_this.blank) _this.graph.addChannel(pack);
			else if(pack.type=='regular'){ _this.graph.addRow(pack);}
			else if(pack.type=='avg'){ _this.graph.curRow.addAvg('AVG', pack);}
			else if(pack.type=='min'){ _this.graph.curRow.addAvg('MIN',pack);}
			else if(pack.type=='max'){ _this.graph.curRow.addAvg('MAX',pack);}
			print(pack.type);
			//else if(pack.type=='max'){ _this.graph.addMax(pack);}
			_this.packs.push(pack);
			
			
		}
		_this.graph.paintAll();
	}
	
	this.ready = function(){
	}
	this.setGraph = function (graph){ this.graph = graph; }
	this.addChannel = function (mp){
		var channel = new Channel(mp);
	}
	this.splitJsons = function(text){
		var ret = [], low = 0, stack = 0, start=true;
		for (var i=0; i< text.length; i++){
			if (text[i]=='{'){
				if (start) low=i;
				stack++;
				start = false;
			}
			if (text[i]=='}' && stack >0) stack--;
			if (!start && stack == 0) {
				valid = ret.push(text.substring(low, i+1));
				low = i+1;
				start=true;
			}
		}
		if (stack !=0) {
			var ogryz = text.substring(low, text.length).length;
			//console.log('OGRYZOK:',ogryz);
			this.segment.back = ogryz;
		}
		return ret;
	}
	this.sleep = function(millis){
		  var date = new Date();
		  var curDate = null;
		  do { 
			  curDate = new Date(); 
		  } while (curDate-date < millis);
	}
	function print(txt) {$("#monitor").html(txt)}
	function printa(txt) {$("#mon1").html(txt)}
	function printb(txt) {$("#mon2").html(txt)}
	function appa(txt) {$("#mon1").append("<br>"+txt)}
	function appb(txt) {$("#mon2").append("<br>"+txt)}
	function app(txt) {$("#monitor").append("<br>"+txt)}
	function semafor(boo) {
		if (boo) $("#mon2").css("background-color", "#cea");
		else $("#mon2").css("background-color", "#f99");
	}
};